package com.esb.transport.protocal;

import java.io.IOException;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.esb.transport.handler.codec.serialization.Codec;
import com.esb.utils.Constants;

public class FastjsonCodec implements Codec {

	public byte[] encode(Object message) throws IOException {
		if(message==null)return null;
		String v = JSON.toJSONString(message, SerializerFeature.WriteClassName);
		
		return v.getBytes(Constants.DEFAULT_CHARSET);
		//return ByteUtils.merge(v.getBytes(Constants.DEFAULT_CHARSET), ByteUtils.string2bytes(message.getClass().getName()));
	}

	public Object decode(byte[] data) throws IOException {
		return JSON.parse(new String(data, Constants.DEFAULT_CHARSET));
		/*String className = ByteUtils.bytes2string(data);
		try {
			return JSON.parseObject(new String(ByteUtils.slice(data, 1), Constants.DEFAULT_CHARSET), Class.forName(className));
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return null;*/
	}
}
